import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javaactor.library.core.Message;
import pyx.crazycards.game.cards.Play;
import pyx.crazycards.game.cards.PlayMove;
import pyx.crazycards.server.messages.AuthenticationMsg;
import pyx.crazycards.server.messages.PlayMsg;

public class ClienteListerner extends Thread {
	
	public int port = 8889;
	
	//
	// public static void main(String[] args)
	// throws ActorInvalidDefinitionException {
	// new ClienteListerner().start();
	// }
	
	@Override
	public void run() {
		try {
			ServerSocket server = new ServerSocket(port);
			
			while (true) {
				Socket sock = server.accept();
				
				ObjectInputStream stream = new ObjectInputStream(sock.getInputStream());
				Message msg = (Message) stream.readObject();
				
				if (msg instanceof AuthenticationMsg) {
					System.out.println(((AuthenticationMsg) msg).getToken());
				} else if (msg instanceof PlayMsg) {
					PlayMsg pm = (PlayMsg) msg;
					
					for (Play pl : pm.getPlays()) {
						if (pl.isEnemy())
							System.out.println("OPPONENT");
						else
							System.out.println("PLAYER");
						
						for (PlayMove mv : pl.getMoves()) {
							System.out.println("   MOVE: " + mv.getType());
							
							System.out.println("           Target:" + mv.getTarget() + " SubId:" + (mv.getTarget()!=null?""+mv.getTarget().getSubId() : " "));
							System.out.println("           Origin:" + mv.getOrigin() + " SubId:" + (mv.getOrigin()!=null?""+mv.getOrigin().getSubId() : " "));
						}
					}
					
					System.out.println("++++++++++++++++++++++++++++++++++++++");
				}
				
			}
			
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}
